package com.zb.zbtcommon.controller;

import com.zb.zbtcommon.dto.ResponseEntity;
import com.zb.zbtcommon.entity.News;
import com.zb.zbtcommon.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zbt/common/news")
@Api(value = "NewsController|一个用来测试swagger注解的控制器")
public class NewsController {

    private Logger logger = LoggerFactory.getLogger(NewsController.class);

    @Autowired
    private NewsService newsService;

    @RequestMapping("/findNews")
    @ApiOperation(value = "/findNewsSwagger")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "newstypeid", value = "用户编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType="query", name = "newstypeid", value = "用户编号", required = true, dataType = "Integer")
    })
    public ResponseEntity<Map<String, Object>> findNews(String newstypeid,int currentPage){
        ResponseEntity<Map<String, Object>> entity =null;
        try {
            entity =new ResponseEntity<>();
            //列表
            List<News> newslist =newsService.findNews(newstypeid, currentPage);
            //数据总数
            int count = newsService.findNewsCount(newstypeid);
            //获取总页数
            int totalPage = count % 5 == 0 ? count/5 :count/5+1;
            //封装
            Map<String , Object> result= new HashMap<String , Object>();
            result.put("newslist", newslist);
            result.put("count", count);
            result.put("totalPage", totalPage);
            entity.setCode(HttpStatus.OK.value());
            entity.setTarget(result);
        } catch (Exception e) {
            logger.error("地址：/zbt/common/listnewsById-----------------请求参数：newstypeid"+newstypeid+"       异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("查询新闻列表错误"+e.getMessage());
        }
        return entity;
    }
    @RequestMapping("/findNewsInnovation")
    public ResponseEntity<List<News>> findNewsInnovation(){

        ResponseEntity<List<News>> entity= null;
        try {
            entity= new ResponseEntity<>();
            List<News> list= newsService.findNewsInnovate();
            entity.setCode(HttpStatus.OK.value());
            entity.setTarget(list);
        } catch (Exception e) {
            logger.error("地址：/zbt/common/listnewInnovation-----------------请求参数：无     异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("查询创新中心前16条数据错误");
        }
        return entity;
    }
    @RequestMapping("/findNewsQualification")
    public ResponseEntity<List<News>> findNewsQualification(){

        ResponseEntity<List<News>> entity= null;
        try {
            entity= new ResponseEntity<>();
            List<News> list= newsService.findNewsQualification();
            entity.setCode(HttpStatus.OK.value());
            entity.setTarget(list);
        } catch (Exception e) {
            logger.error("地址：/zbt/common/findNewsQualification-----------------请求参数：无     异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("查询资质认证前4条数据错误");
        }
        return entity;
    }
    @RequestMapping("/findNewsTechnolog")
    public ResponseEntity<List<News>> findNewsTechnolog(){
        ResponseEntity<List<News>> entity= null;
        try {
            entity= new ResponseEntity<>();
            List<News> list= newsService.findNewsTechnolog();
            entity.setCode(HttpStatus.OK.value());
            entity.setTarget(list);
        } catch (Exception e) {
            logger.error("地址：/zbt/common/findNewsTechnolog-----------------请求参数：无     异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("查询资质认证前4条数据错误");
        }
        return entity;
    }
    @RequestMapping("/findNewsIntellectual")
    public ResponseEntity<List<News>> findNewsIntellectual(){
        ResponseEntity<List<News>> entity= null;
        try {
            entity= new ResponseEntity<>();
            List<News> list= newsService.findNewsIntellectual();
            entity.setCode(HttpStatus.OK.value());
            entity.setTarget(list);
        } catch (Exception e) {
            logger.error("地址：/zbt/common/findNewsIntellectual-----------------请求参数：无     异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("查询资质认证前4条数据错误");
        }
        return entity;
    }

    @RequestMapping("/findNewsById")
    public ResponseEntity<News> findNewsById(String newsid){
        ResponseEntity<News> entity =null;
        try {
            entity = new ResponseEntity<>();
            News news=newsService.findNewsById(newsid);
           if(news!=null){
               entity.setCode(HttpStatus.OK.value());
               entity.setTarget(news);
           }
        } catch (Exception e) {
            logger.error("地址：/zbt/common/listnewsByIdnewId-----------------请求参数：newsid"+newsid+"       异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("根据newID查询news对象数据错误");
        }
        return entity;
    }

    @RequestMapping("/pageList")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> pageList(String newstypeid){
        ResponseEntity<Map<String , Object>> entity= null;
        //获取总页数
        try {
            entity= new ResponseEntity<>();
            int count = newsService.findNewsCount(newstypeid);
            List<News> list= newsService.pageList(newstypeid);
            int totalPage = count % 5 == 0 ? count/5 :count/5+1;
            entity.setCode(HttpStatus.OK.value());
            Map<String , Object> result= new HashMap<String , Object>();
            result.put("pageList", list);
            result.put("count", count);
            result.put("totalPage", totalPage);
            entity.setCode(HttpStatus.OK.value());
            entity.setTarget(result);
        } catch (Exception e) {
            logger.error("地址：/zbt/common/pageList-----------------请求参数：无     异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("分页列表错误");
        }
        return entity;
    }
    /* -------------------------------------后台页面----------------------------------------------------------------------*/
    @RequestMapping("/saveNews")
    public ResponseEntity<String> saveNews(News news){
        ResponseEntity<String> entity= new ResponseEntity<>();

        int count= newsService.saveNews(news);
        try {
            if(count>0){
                entity.setCode(HttpStatus.OK.value());
                entity.setTarget("保存成功");
            }else{
                entity.setCode(HttpStatus.BAD_REQUEST.value());
                entity.setTarget("保存失败");
            }
        } catch (Exception e) {
            logger.error("/zbt/common/news/saveNews------------请求对象为News"+news+"       异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("新增数据错误");
        }
        return entity;
    }
    @RequestMapping("/updateNews")
    public  ResponseEntity<String> updateNews(News news,String newsid){
        ResponseEntity entity =new ResponseEntity<>();
        int count =newsService.updateNews(news);
        try {
            if(count>0){
                entity.setCode(HttpStatus.OK.value());
                entity.setTarget("修改成功！");
            }else{
                entity.setCode(HttpStatus.BAD_REQUEST.value());
                entity.setTarget("修改失败！");
            }
        } catch (Exception e) {
            logger.error("/zbt/common/news/updateNews------------请求对象为Newsid\"+newsid+\"       异常信息\"+e.getMessage()");
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("修改数据错误");
        }
        return  entity;
    }
    @RequestMapping("/deleNews")
    public  ResponseEntity<String> deleNews(String newsid){
        ResponseEntity entity =new ResponseEntity();
        int count=newsService.deleNews(newsid);
        try {
            if(count>0){
                entity.setCode(HttpStatus.OK.value());
                entity.setMessage("删除成功");
            }else{
                entity.setCode(HttpStatus.BAD_REQUEST.value());
                entity.setMessage("删除失败");
            }
        } catch (Exception e) {
            logger.error("/zbt/common/news/deleNews   请求参数+newsid"+newsid+"     异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            entity.setMessage("删除数据出错");

        }
        return  entity;
    }
    @RequestMapping("findNewsList")
    public ResponseEntity<Map<String , Object>> findNewsList(String title,String newstypeid,int currentPage){
        ResponseEntity entity = null;
        try {
            List<News> list =newsService.findNewsList(title, newstypeid, currentPage);
            int count =newsService.findNewsCount(newstypeid);
            //总页数 算法  count %  一页最大显示数 ==0 ： count/10 ？page/10 +1
            int total= count% 10==0 ?  count /10 :count /10+1;
            Map<String,Object> result= new HashMap<String , Object>();
            result.put("list", list);
            result.put("count", count);
            result.put("total", total);
            entity = new ResponseEntity();
            entity.setCode(HttpStatus.OK.value());
            entity.setTarget(list);
        } catch (Exception e) {
            logger.error("/zbt/common/news/findNewsList  请求数据 title"+title+" newstypeid"+newstypeid+"  异常信息"+e.getMessage());
            e.printStackTrace();
            entity.setCode(HttpStatus.OK.value());
            entity.setMessage("查询数据错误");
        }
        return entity;
    }

}
